package studentManager;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    Scanner scanner = new Scanner(System.in);
    ArrayList<Student> stuList = new ArrayList<>();
    boolean bool;
    public void addStu() {
        System.out.println("请输入学生学号");
        while (true) {
            int stuNum = scanner.nextInt();
            bool=compareStuNum(stuNum);
            if (bool){
                System.out.println("请输入学生姓名");
                String name = scanner.next();
                System.out.println("请输入学生年龄");
                int age = scanner.nextInt();
                System.out.println("请输入学生性别");
                String sex = scanner.next();
                Student student = new Student(stuNum, name, age, sex);
                stuList.add(student);
                System.out.println("添加成功！");
                break;
            }else {
                System.out.println("学号已存在,请重新输入");
            }
        }

    }
    public boolean compareStuNum(int stuNum){
        for (int i = 0; i <stuList.size() ; i++) {
            if (stuNum==stuList.get(i).getStuNum()){
                return false;
            }
        }
        return true;
    }
    public void printStu(){
        if (stuList.size()==0){
            System.out.println("数据为空，请添加数据");
        }else {
            for (int i = 0; i <stuList.size() ; i++) {
                Student stu= stuList.get(i);
                System.out.println(stu.getStuNum() +stu.getName()+stu.getAge()+stu.getSex());
            }
        }
    }


    public void removeStu() {
        boolean bool=true;
        System.out.println("请输入需要删除信息的学生学号");
        int stuNum = scanner.nextInt();
        if (stuList.size() == 0) {
            System.out.println("目前没有信息，请录入");
        } else {
            for (int i = 0; i < stuList.size(); i++) {
                if (stuList.get(i).getStuNum() == stuNum) {
                    stuList.remove(i);
                    bool=false;
                    System.out.println("删除成功！");
                }
            }
            if (bool){
                System.out.println("查无此人，请重试");
            }
        }


    }

    public void setStu() {
        System.out.println("请输入需要修改信息的学生学号");
        int stuNum = scanner.nextInt();
        boolean bool=true;
        if (stuList.size() == 0) {
            System.out.println("目前没有信息，请录入");
        } else {
            for (int i = 0; i < stuList.size(); i++) {
                if (stuList.get(i).getStuNum() == stuNum) {
                    System.out.println("请输入学生姓名");
                    String name= scanner.next();
                    stuList.get(i).setName(name);
                    System.out.println("请输入学生年龄");
                    int age = scanner.nextInt();
                    stuList.get(i).setAge(age);
                    System.out.println("请输入学生性别");
                    String sex = scanner.next();
                    stuList.get(i).setSex(sex);
                    System.out.println("修改成功！");
                    bool=false;
                }
            }
            if (bool){
                System.out.println("查无此人，请重试");
            }

        }


    }
    public void getStu(){
        boolean bool=true;
        System.out.println("请输入需要查找信息的学生学号");
        int stuNum = scanner.nextInt();
        if (stuList.size() == 0) {
            System.out.println("目前没有信息，请录入");
        } else {

            for (int i = 0; i < stuList.size(); i++) {
                if (stuList.get(i).getStuNum() == stuNum) {
                    bool=false;
                    Student stu=stuList.get(i);
                    System.out.println(stu.getStuNum() +stu.getName()+stu.getAge()+stu.getSex());
                }
            }
            if (bool){
                System.out.println("查无此人，请重试");
            }

        }


    }
}